import { _decorator, Component, instantiate, Node, Prefab, ScrollView } from 'cc';
import MessageApi from '../api/MessageApi';
import { Message } from '../entity/Message';
import { MessageItem } from './MessageItem';
import { UIManager } from '../common/UIManager';
const { ccclass, property } = _decorator;

@ccclass('MessageList')
export class MessageList extends Component {
    @property(ScrollView)
    scrollView:ScrollView
    @property(Node)
    content:Node
    @property(Node)
    empty:Node
    @property(Prefab)
    messageItem:Prefab

    private page:number = 0

    start() {
        this.scrollView.node.on('scroll-to-bottom', this.scrollToBottom, this);
        this.getList(1)
    }

    async getList(page:number){
        this.page = page
        await UIManager.instance.showLoading()
        let res = await MessageApi.getList({
            page:0,
            mode: 0
        })
        UIManager.instance.closeLoading()
       
        for(let item of res.data){
            let m = Message.createByObj(item)
            let node = instantiate(this.messageItem)
            this.content.addChild(node)
            node.getComponent(MessageItem).setData(m)
        }

        if(res.data.length > 0){
            this.empty.active = false
        }
    }

    scrollToBottom(){
      
        this.page ++
        this.getList(this.page)
    }
    
}


